/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ucjava.chat.commserve.rmi.common;

import java.util.prefs.Preferences;

/**
 * Wraps java.util.prefs.Preferences
 *
 * The java.util.prefs.Preferences class takes care of storage location.
 *
 * Use Singleton pattern -- only one copy of this class can exist at a time --
 * if its constructor is called again, the constructor returns the
 * already-existing instance.
 *
 * Accessors in PrefStore return what is stored in the Preferences object. If
 * that object doesn't have what is requested, the default value for that key is
 * returned.
 *
 *
 *
 * @author tony
 */
public class PrefStore
{

    // Initialize this to null so we'll know
    // that factory hasn't already been called.
    private static PrefStore oneAndOnly = null;
    private Preferences prefs;
    /////////////////////////////////////////////////////
    //////////////// DEFAULT SETTINGS ///////////////////
    /////////////////////////////////////////////////////
    // Other strings
    // Times:
    // After how many milliseconds are inactive
    // clients logged out?
    private static final long DEFAULT_CLIENT_INACTIVITY_ALLOWANCE = 30 * 60 * 1000; // 30 MINUTES

    public long getClientInactivityAllowance()
    {
        return prefs.getLong("clientInactivityAllowance",
                DEFAULT_CLIENT_INACTIVITY_ALLOWANCE);
    }

    public void setClientInactivityAllowance(long L)
    {
        prefs.putLong("clientInactivityAllowance", L);
    }
    // How often to check for inactive clients:
    private static final long DEFAULT_CLIENT_CHECK_INTERVAL = 60 * 10 * 1000; // 10 MINUTES

    public long getClientCheckInterval()
    {
        return prefs.getLong("clientCheckInterval", DEFAULT_CLIENT_CHECK_INTERVAL);
    }

    public void setClientCheckInterval(long L)
    {
        prefs.putLong("clientCheckInterval(", L);
    }
    // What to display after server initialization is finished:
    private static final String DEFAULT_SERVER_READY_MSG = "Server ready";

    public String getServerReadyMsg()
    {
        return prefs.get("serverReadyMsg", DEFAULT_SERVER_READY_MSG);
    }

    public void setServerReadyMsg(String s)
    {
        prefs.put("serverReadyMsg", s);
    }
    
    // Time in milliseconds to sleep between sending out of
    // messages:
    private static final long DEFAULT_MSG_BROADCAST_INTERVAL = 1000;
    public long getMsgBroadcastInterval()
    {
        return prefs.getLong("msgBroadcastInterval", DEFAULT_MSG_BROADCAST_INTERVAL);
    }
    public void setMsgBroadcastInterval(long L)
    {
        prefs.putLong("msgBroadcastInterval", L);
    }

    
    // How long (in milliseconds) between "cullings" of
    // message archive -- ten minutes
    private static final long DEFAULT_ARCHIVE_CHECK_INTERVAL
            = 10 * 60 * 1000;
    public long getArchiveCheckInterval()
    {
        return prefs.getLong("archiveCheckInterval", DEFAULT_ARCHIVE_CHECK_INTERVAL);
    }
    public void setArchiveCheckInterval(long L)
    {
        prefs.putLong("archiveCheckInterval", L);
    }
    
    // How long (in milliseconds) do messages stay in the archiving
    // queue before they're deleted or moved to persistent storage?
    // Set to 13 minutes initially. No good reason for that number,
    // except that it's prime -- that way the ten-minute stuff, the
    // thirty-minute stuff, and this won't happen at once so often.
    private static final long DEFAULT_ARCHIVE_WAIT
            = 13 * 60 * 1000;
    public long getArchiveWait()
    {
        return prefs.getLong("archiveWait", DEFAULT_ARCHIVE_WAIT);
    }
    public void setArchiveWait(long L)
    {
        prefs.putLong("archiveWait", L);
    }
   
    /////////////////////////////////////////////////////
    //////////////// END DEFAULT SETTINGS ///////////////
    /////////////////////////////////////////////////////


    /**
     * Constructor
     */
    private PrefStore()
    {
        prefs = Preferences.userNodeForPackage(getClass());
    } // end constructor

    /**
     * public method to return reference to one and only instance of this class.
     */
    public static PrefStore makePrefStore()
    {
        if (oneAndOnly == null)
          {
            oneAndOnly = new PrefStore();
          }
        return oneAndOnly;
    }
} // end class PrefStore
